package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussPropertyFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussPropertyInfo;
import cn.pegasus.teamwork.pegasus.entity.BussUcenterArea;
import cn.pegasus.teamwork.pegasus.mapper.BussPropertyInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussPropertyFunddetailsService;
import cn.pegasus.teamwork.pegasus.service.BussPropertyInfoService;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;

/**
 * 物业信息
 * @author 40854
 *
 */
@Controller
@RequestMapping("/property")
public class PropertyController extends BaseManagerController<BussPropertyInfo, BussPropertyInfoMapper>{

	private static final Logger logger = LoggerFactory.getLogger(PropertyController.class);
	@Autowired
	private BussPropertyInfoService propertyInfoService;
	@Autowired
	private BussUcenterAreaService ucenterAreaService;
	@Autowired
	private BussPropertyFunddetailsService propertyFunddetailsService;
	/**
	  * 查询物业列表
	  */
	 @RequestMapping(value = "/list",method = {RequestMethod.GET})
	 public ModelAndView list(@RequestParam Map<String, Object> map){
		 ModelAndView view = new ModelAndView("property/property-list");
		 if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		 try{
			 PageObject<BussPropertyInfo> dicList = propertyInfoService.PageList(map);
			 view.addObject("list", dicList);
			 view.addObject("map", map);
		 }catch(Exception e){
			 logger.error("查询评论列表异常",e.getMessage());
		 }
		 return view;
	 }
	 
	 /**
	  * 物业结算提款
	  */
	 @RequestMapping(value = "/listAdu",method = {RequestMethod.GET})
	 public ModelAndView listAdu(@RequestParam Map<String, Object> map){
		 ModelAndView view = new ModelAndView("property/propertyAdu-list");
		 if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		 try{
			 PageObject<BussPropertyInfo> dicList = propertyInfoService.PageListAdu(map);
			 view.addObject("list", dicList);
			 view.addObject("map", map);
		 }catch(Exception e){
			 logger.error("查询评论列表异常",e.getMessage());
		 }
		 return view;
	 }
	 /**
	  * 跳转新增页面
	  * type  1  编辑
	 * @throws Exception 
	  */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAddArea(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Long id) throws Exception{
		 List<BussUcenterArea> areaList = ucenterAreaService.findList(null, 4);
		 if(null == type){
			 ModelAndView view = new ModelAndView("property/property-add");
			 view.addObject("areaList", areaList);
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("property/property-edit");
			 BussPropertyInfo info = propertyInfoService.findPropertyInfo(id);
			 view.addObject("info", info);
			 view.addObject("areaList", areaList);
			 return view;
		 }
	 }
	 /**
	  * 新增
	  */
	 @ResponseBody
	 @RequestMapping(value = "/addProperty")
	 public String addProperty(@RequestParam Map<String, Object> map){
        try {
        	propertyInfoService.addProperty(getAdmin().getId(), map);
        } catch (Exception e) {
            logger.error("新增物业信息异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 修改区域信息
	  * @param map
	  * @return
	  * id 
	  * areaName
	  * longitude
	  * latitude
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateProperty")
	 public String updateProperty(@RequestParam Map<String, Object> map){
        try {
        	propertyInfoService.updateProperty(getAdmin().getId(), map);
        } catch (Exception e) {
            logger.error("修改物业信息异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/deleteProperty")
	 public Map<String,Object> deleteProperty(@RequestParam(value="id",required = false) Long id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	propertyInfoService.deleteProperty(getAdmin().getId(),id);
        	resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("删除物业异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
	 
	 /**
	  * 查询物业资金流水列表
	  */
	 @RequestMapping(value = "/listFunddetails",method = {RequestMethod.GET})
	 public ModelAndView listFunddetails(@RequestParam Map<String, Object> map){
		 ModelAndView view = new ModelAndView("property/funddetails-list");
		 if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		 try{
			 PageObject<BussPropertyFunddetails> dicList = propertyFunddetailsService.PageList(map);
			 view.addObject("list", dicList);
			 view.addObject("map", map);
		 }catch(Exception e){
			 logger.error("查询评论列表异常",e.getMessage());
		 }
		 return view;
	 }
	 
	 /**
	  * 申请提款
	  */
	 @ResponseBody
	 @RequestMapping(value="/drawing")
	 public String drawing(@RequestParam(value="balance",required = false)Double balance,
			 @RequestParam(value="id",required = false)Long id){
		 if(id==null || balance == null){
			 return "1001";
		 }
		 try{
			 propertyInfoService.drawing(id, balance);
		 }catch(Exception e){
			 logger.error("物业提现申请异常", e);
			 return "1001";
		 }
		 return "200";
	 }
	 /**
	  * 跳转提款输入页面
	  */
	 @RequestMapping(value="/addDraw")
	 public ModelAndView addDraw(@RequestParam(value="balance",required = false)Long balance,
			 @RequestParam(value="id",required = false)Long id){
		 ModelAndView view = new ModelAndView("property/draw-edit");
		 view.addObject("balance", balance);
		 view.addObject("id", id);
		 return view;
	 }
	 /**
	  * 确认打款
	  */
	 @ResponseBody
	 @RequestMapping(value="/play")
	 public Map<String,Object> play(@RequestParam(value="balance",required = false)Long balance,
			 @RequestParam(value="id",required = false)Long id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	propertyInfoService.play(getAdmin().getId(),id,balance);
        	resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("删除物业异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
}
